//
//  CinemaViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaCell.h"
#import "AppTools.h"
#import "AFNetworking.h"
#import "Cinema.h"
#import "MBProgressHUD.h"
@interface CinemaViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *mytableView;
@property(nonatomic, retain)NSMutableArray *cinArray;
@property (nonatomic, retain) MBProgressHUD *hub;


@end

@implementation CinemaViewController

- (void)dealloc
{
    [_hub release];
    [_cinArray release];
    [_mytableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"影院";
    
    //创建tableView
    self.mytableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    [self.view addSubview:self.mytableView];
    [_mytableView release];
    
    
    //注册cell
    [self.mytableView registerClass:[CinemaCell class] forCellReuseIdentifier:@"cinema"];
    
    //初始化
    self.cinArray = [NSMutableArray array];
    
    //请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
    //让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"玩命加载ing......";
    [self.view addSubview:self.hub];

    
    
    [AppTools getDataWithUrlStr:urlStr andBlock:^(id result) {
        
        NSMutableDictionary *bigDic = result;
        NSString *reason = [bigDic objectForKey:@"reason"];
        NSMutableDictionary *dic = [bigDic objectForKey:@"result"];
        NSMutableArray *array = [dic objectForKey:@"data"];
        if ([reason isEqualToString:@"success"]) {
            for (NSMutableDictionary *dic2 in array) {
                
                Cinema *cinema = [[Cinema alloc]init];
                //把字典存入model
                [cinema setValuesForKeysWithDictionary:dic2];
                
                //把model放入数组中
                [self.cinArray addObject:cinema];
                
                [cinema release];
            }
            
            //让菊花停止
            [self.hub hide:YES];
            
//            NSLog(@"====%@",self.cinArray);
            
            [self.mytableView reloadData];
        }

        
    } error:^(NSError *error) {
        
        //让菊花停止
        [self.hub hide:YES];

    }];
    
    
}

#pragma mark -- 设置行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cinArray.count;
}


#pragma mark -- 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cinemaCell = @"cinema";
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:cinemaCell];
    
    Cinema *cin = [self.cinArray objectAtIndex:indexPath.row];
    cell.mycinema = cin;
    
    NSLog(@"---%@",cell.mycinema);
    
    return cell;
}


#pragma mark -- 设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

#pragma mar -- 触摸cell时触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
